package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102500761102";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDIk4/L93zdNzT23LKsIQUGhKhrV6fiOkW4+f+b1aRUeEX14jRMEwVvs9o3IIImvAqxgAtNSgozvrErDpsFg5vNE0cz7gBxkbUI8DsK8t6yvmIrbVipTla1290siHG9Cs4pt0KeYq9oWgI18C5ACX6rrr33gxTS9mVpemZf0tAgp6cr7Dc8bdS62mNgAmxP/PS0lqJkFZ5os11ndloe/e/BirttAHUKBSCGP6jBtcm9isYtdswIy16aLbuYJsQO4usHlUXdxRAe+FcApTHGqwtrFFeuM+EgPJRj9+W9qSUpn+KL4RN6qDzLYlClnO+W3rLADxWfp+I+13uEbQWGhEjBAgMBAAECggEBAIAbT2KUxZl840UwnKW3yT/qhq9Yow+o7FNCyXE8uOKClxx+x6w3EefQVXWUO8yFmcbY418QBQgbqYi4oWaaMm5++6E9R+dBiwEUfoF1sas0UfsuscwhDtzEerUwyIRXXGnkdJY4rZPWuUa+Cqw35EG/L/HgNPKxEQCXfr+BJBFWICgV86eJC2NjiNWFlr1HCItEqVnGTuDClZS1oQJiLo72W7ECxWFQ25jsaeWIBm0ITUxOrYJ3O5fM7Z6LNtRVw7Pwn3GD1Fgt+u3FauSK/KLVVrit4GTbDwmGTgVce9y4ZJg/jqxXLIvUYxWLbj3A8dg5KD7g9lzUf68k5DE8pmECgYEA++ng0WXwer1b2ZPEM9q6zdymC2Ux61glfHftROXXtUhMtJMxThxfVz2YOOHyHh8NrDkwWuvtJoGIsF4JfbvsD5B2avohbgoRFokYRoRU2w17Oq8jW2nXnXWnW/bTcllHeyN/pw1EBBTpRLSR4KjGd1xoXHqqBRWZaUk/3ux4yaUCgYEAy9R+3i2ArEVmSx2Ow/syxwR7EyiHWFvuF5eCAJzOmAQBvopDRDX18rbgARQXtA12sJwlHHlGZbPTppMSRRdnY9C6m1HNswKtCKR2sI8OMogg2kWHSW31+T055lyJfJhSbUApT3zHspWpTtA7+YeC4j+u/TPCpFuVWrs9eNwqP+0CgYBsnYWao4pRUY3lvQmOlhOPKUDKjwI8B9B53zeFSlh8SEUSYpiSpjcRZXRZqkn7ZMY/aJUJzsl8ncMy0Ajlln4Mo+v66gg56ss9XTUDSpFPtXtZbSQgiP03XgAf7cdzeT0RwdeLTnPyEpYVUonHRN31nvk8ToM6F/sCY1vPoUPfJQKBgAURAhpJrUIa84D4ehz6+Hcj8F84iayzlEZ32J5lR2lV6CkxTJy8bqGa1BSSyEgMcZ/QMcf0Z8CNaLxiOXkILOS5Qnor3V4OufhWBjE7TRIKei6SvdsgZZXNZMwKDszPCHV4u84i/t+8gpnX6TW5nZI8HQBkMfld4WeA8fU4G8C9AoGBAPZ1LFTQq3l0IxZlDisKs5m3CiZfuB411GjRZhB7gD5MIhkf0cDoTNOKuWDYiL9gz0EFwNNSYXSfrYjtxvll/74IirH0bJIHBgdROUkOKLFYTY+ZhoCAuQrBHgudj/OF0D4y3oH3WPBtsUdVlDBv4AuowcT0VXy/vS66CK3qFZvI";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApcVZD/n/+G6q1Ia+oFmXikOnYUC96+Knt64E5+9ZeaYV+NuSqYPyNmXyQ0quclS0isLIzB2xb+31pfPdkkpZQqMfQUkmuTpJFfAf+oYis5vjZNDoSW1Tg+tZj3GL+qKC4vN4ikWXUAzD9xTZGtBsTPiHbv3KONe26GyR3KFbxfbA8+gT2r3/aWa8C5UdskOXlgS3fVtqYk8DMNI0iBfk03c63mAmvIL4AeaTlQRWYPzoP6XFLKqzBZAGsQLx5S2mrZb2PwlEkoxNE1+SBnTB8AUQvRkmZygz/IzHYh71qHeBINeuVIZzfVP7H0rj8iHUG7JurcmScgq2LEv4nYzLHQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    public static String notify_url = "http://kpvxrllc4p.54http.tech/payed/notify";


    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    //收单时间
    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}